// let a = {
//     age: 1
// }

// let b = a
// a.age = 2

// 浅拷贝
let a = {
    ages: 1
}

// 浅拷贝

// let b = Object.assign({}, a)

// let b = {...a}

// 深拷贝
// let b = JSON.parse(JSON.stringify(a))


// let obj = {
//     a: 1,
//     b: undefined
// }

// let obj2 = JSON.parse(JSON.stringify(abj))


function deepClone(obj, map = new WeakMap()) {
    if(obj == null) return
    if(typeof obj !== 'object') return obj
    if(map.get(obj)) return map.get(obj)
    let newClone = new obj.constructor()
    map.set(obj, newClone)
    for(let i in obj) {
            newClone[i] = obj[i] instanceof Object ? deepClone(obj[i], map) : obj[i]
    }
    return newClone
}




let obj = {
    a: [1, 2, 3],
    b: {
        c: 1,
        d: 2
    },
    
}
obj.z = obj
let newObj = deepClone(obj)
console.log(newObj.a, newObj.b.c, newObj.z);